﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RFIDSystem.DAL.Common
{
    public static class FormHelper
    {
        public static void ShowForm<T>(GroupBox c)
           where T : BaseForm, new()
        {
            ShowForm<T>(c, true);
        }

        public static void ShowForm<T>(GroupBox c,bool isClosePreForm)
            where T : BaseForm, new()
        {
            
            if (c.Controls.Count>0 && isClosePreForm)
            {
                var pform = c.Controls[0] as Form;
                if (pform != null && pform.Name!= "IPConfig")
                {
                    pform.Close();                    
                }
            }
           
            c.Controls.Clear();

            BaseForm form = isClosePreForm ? new T() : Singleton<T>.GetInstance();
            form.TopLevel = false;
            form.FormBorderStyle = FormBorderStyle.None;
            form.Width = c.Width * 7 / 10;
            form.Height = c.Height * 7 / 10;
            form.Top = 20;
            form.Left = 10;
            c.Text = form.Text;
            c.Controls.Add(form);
            form.Show();
            


        }
    }


    public class Singleton<T> where T : BaseForm, new()
    {
        private static T _instance;
        private static readonly object SyncObject = new object();
        public static T GetInstance()
        {
            if (_instance == null)
            {
                lock (SyncObject)
                {
                    if (_instance == null)
                    {
                        _instance = new T();                      
                    }
                }
            }
            return _instance;
        }
     
    }
}
